package com.zjj.edu.system.service.impl;

import com.zjj.edu.common.constant.UserConstants;
import com.zjj.edu.common.utils.DateUtils;
import com.zjj.edu.common.utils.StringUtils;
import com.zjj.edu.system.domain.SysSubject;
import com.zjj.edu.system.mapper.SysSubjectMapper;
import com.zjj.edu.system.service.ISysSubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 学科Service业务层处理
 *
 * @author zjj
 * @date 2020-06-02
 */
@Service
public class SysSubjectServiceImpl implements ISysSubjectService
{
    @Autowired
    private SysSubjectMapper sysSubjectMapper;

    /**
     * 查询学科
     *
     * @param id 学科ID
     * @return 学科
     */
    @Override
    public SysSubject selectSysSubjectById(Long id)
    {
        return sysSubjectMapper.selectSysSubjectById(id);
    }

    /**
     * 查询学科列表
     *
     * @param sysSubject 学科
     * @return 学科
     */
    @Override
    public List<SysSubject> selectSysSubjectList(SysSubject sysSubject)
    {
        return sysSubjectMapper.selectSysSubjectList(sysSubject);
    }

    @Override
    public List<SysSubject> selectAllSysSubjectList(Long tenantId) {
        return sysSubjectMapper.selectAllSysSubjectList(tenantId);
    }

    /**
     * 新增学科
     *
     * @param sysSubject 学科
     * @return 结果
     */
    @Override
    public int insertSysSubject(SysSubject sysSubject)
    {
        sysSubject.setCreateTime(DateUtils.getNowDate());
        return sysSubjectMapper.insertSysSubject(sysSubject);
    }

    /**
     * 修改学科
     *
     * @param sysSubject 学科
     * @return 结果
     */
    @Override
    public int updateSysSubject(SysSubject sysSubject)
    {
        sysSubject.setUpdateTime(DateUtils.getNowDate());
        return sysSubjectMapper.updateSysSubject(sysSubject);
    }

    /**
     * 删除学科对象
     *
     * @return 结果
     */
    @Override
    public int deleteSysSubjectByIds(SysSubject sysSubject)
    {
        return sysSubjectMapper.deleteSysSubjectByIds(sysSubject);
    }

    @Override
    public String checkNameUnique(SysSubject vo) {
        Long id = StringUtils.isNull(vo.getId()) ? -1L : vo.getId();
        SysSubject info = sysSubjectMapper.checkNameUnique(vo.getName(), vo.getTenantId());
        if (StringUtils.isNotNull(info) && info.getId().longValue() != id.longValue())
        {
            return UserConstants.NAME_NOT_UNIQUE;
        }
        return UserConstants.NAME_UNIQUE;
    }
}
